/*
' Copyright (c) 2012  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using DotNetNuke.Modules.Dnn7TmDal1.Components;
using System.Web.UI.WebControls;
using System.Data;

namespace DotNetNuke.Modules.Dnn7TmDal1
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from Dnn7TmDal1ModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : Dnn7TmDal1ModuleBase, IActionable
    {

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {

                    populateDatagrid();
                }

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        private void populateDatagrid()
        {
            this.GridView1.DataSource = TaskController.GetTasks(ModuleId);
            GridView1.DataBind();
        }

        #region Optional Interfaces

        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString("EditModule", this.LocalResourceFile), "", "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

        protected void GridView1_RowCommand(object sender,
          GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditTask")
            {


                string url = EditUrl(string.Empty, string.Empty, "Edit", "tid=" + SelectedTaskId(e));
                Response.Redirect(url);



            }
            else if (e.CommandName == "DeleteTask")
            {

                TaskController.DeleteTask(SelectedTaskId(e));
                populateDatagrid();
            }

        }

        private class FieldPositions
        {
            public const int TaskId = 3;
        }

        private int SelectedTaskId(GridViewCommandEventArgs e)
        {
            // Retrieve the row index stored in the 
            // CommandArgument property.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button 
            // from the Rows collection.
            GridViewRow row = GridView1.Rows[index];

            // Add code here to add the item to the shopping cart.

            String TaskIdAsString = row.Cells[FieldPositions.TaskId].Text;
            return Convert.ToInt32(TaskIdAsString);
        }


        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //var item = (DataRow)e.Row.DataItem;
                //I'm assuming the type is DataRow from you above example
                //var linkCol = (HyperLinkField)e.Row.Cells[2];
                //linkCol.NavigateUrl = "www.microsoft.com";
                Task curTask = (Task)e.Row.DataItem;

                e.Row.Cells[1].Text = string.Format("<a href=\"{0}\">EDIT</a>", EditUrl(String.Empty, String.Empty, "Edit", "tid=" + curTask.TaskId));
                //e.Row.Cells[2].Text = string.Format("<a href=\"{0}\">EDIT</a>", EditUrl(String.Empty, String.Empty, "Edit", "tid=" + curTask.TaskId));

            }
        }


    }

}
